package cn.getword.cnblogs.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

/*
 *  Copyright : 南京钢铁联合有限公司 信息与自动化公司 NISCO
 *  Author : sunanjun(012169) sunanjun@njsteel.com.cn
 *  System : 系统说明
 *  Target : 此日期工具类默认每周的第一天为周一.
 *  Create : 2013-1-16 下午03:35:00
 *  Update : 2013-1-16 下午03:35:00
 */
public class DateTools {
	/**
	 * @author sunanjun(012169) 2011-1-1 sunanjun@njsteel.com.cn
	 * @function 返回指定格式当前日期,格式:yyyyMMddHHmmss,如果指定格式报错,返回为标准格式
	 * @param strDateFormat
	 * @return
	 */
	public static String getFullNowDateTime(String strDateFormat) {
		try {
			SimpleDateFormat ymd = new SimpleDateFormat(strDateFormat);
			return ymd.format(new Date());
		} catch (Exception e) {
			return DateTools.getFullNowDateTime("yyyy-MM-dd HH:mm:ss");
		}
	}

	/**
	 * @author sunanjun(012169) 2011-1-1 sunanjun@njsteel.com.cn
	 * @function 返回默认格式的字符串日期,格式:yyyyMMddHHmmss
	 * @return
	 */
	public static String getFullNowDateTime() {
		return DateTools.getFullNowDateTime("yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 根据erp的特色生成当前时间
	 * @author WangWeiWei 020463
	 * @date 2019年3月26日 下午8:44:27
	 * @return
	 */
	public static String getFullNowDateTimeErp() {
		return DateTools.getFullNowDateTime("yyyyMMddHHmmss");
	}

	/**
	 * @author sunanjun(012169) 2011-1-1 sunanjun@njsteel.com.cn
	 * @function 返回指定格式当前日期,格式:yyyyMMdd,如果指定格式报错,返回为标准格式
	 * @param strDateFormat
	 * @return
	 */
	public static String getNowDateTime(String strDateFormat) {
		try {
			SimpleDateFormat ymd = new SimpleDateFormat(strDateFormat);
			return ymd.format(new Date());
		} catch (Exception e) {
			return DateTools.getNowDateTime("yyyy-MM-dd");
		}
	}

	/**
	 * @author sunanjun(012169) 2011-1-1 sunanjun@njsteel.com.cn
	 * @function 返回默认格式的字符串日期,格式:yyyyMMdd
	 * @return
	 */
	public static String getNowDateTime() {
		return DateTools.getNowDateTime("yyyy-MM-dd");
	}

	/**
	 * @author sunanjun(012169) 2011-1-1 sunanjun@njsteel.com.cn
	 * @function 返回指定格式当前日期,格式:yyyyMMdd,如果指定格式报错,返回为标准格式
	 * @param inputData
	 * @param strDateFormat
	 * @return
	 */
	public static String getFormatDateTime(String inputData, String strDateFormat) {
		try {
			SimpleDateFormat ymd = new SimpleDateFormat(strDateFormat);
			return ymd.format(ymd.parse(inputData));
		} catch (Exception e) {
			return DateTools.getNowDateTime("yyyy-MM-dd");
		}
	}

	/**
	 * 返回指定格式当前日期,格式:yyyyMMdd,如果指定格式报错,返回为标准格式
	 * @param inputData
	 * @param strDateFormat
	 * @return
	 */
	public static String getFormatDateTimeNew(String inputData, String strOldDateFormat, String strDateFormat) {
		try {
			SimpleDateFormat ymd = new SimpleDateFormat(strOldDateFormat);
			SimpleDateFormat newymd = new SimpleDateFormat(strDateFormat);
			return newymd.format(ymd.parse(inputData));
		} catch (Exception e) {
			return DateTools.getNowDateTime("yyyy-MM-dd HH:mm:ss");
		}
	}

	public static String getFormatDateTime(String inputData) {
		return getFormatDateTime(inputData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * @author sunanjun(012169) 2011-1-1 sunanjun@njsteel.com.cn
	 * @function 返回指定格式当前日期,由old格式，更新为new格式，执行一个替换
	 * @param inputData
	 * @param oldChar
	 * @param newChar
	 * @return
	 */
	public static String getFormatDate(String inputData, String oldChar, String newChar) {

		if (inputData == null)
			return inputData;

		return inputData.replace(oldChar, newChar);
	}

	/**
	 * 得到当前时间下拉列表值
	 * 
	 * @return
	 * @author sunanjun 2013-6-13 下午1:58:14
	 */
	public static Map<String, String> getDayWorkTime() {
		String first = "00";
		String second = "00";
		String label = "";
		String value = "";
		Map<String, String> timeMap = null;
		timeMap = new TreeMap<String, String>();
		for (int i = 5; i < 24; i++) {
			first = StringTools.formatLeftPadZero(String.valueOf(i), 2);
			for (int j = 0; j < 60; j = j + 5) {
				second = StringTools.formatLeftPadZero(String.valueOf(j), 2);
				label = first + ":" + second;
				value = first + ":" + second;
				timeMap.put(value, label);
			}
		}
		return timeMap;
	}

	/**
	 * 根据间隔时间得到每隔多少分钟的时间，比如传入5时,即得到当前时间为：05,10,15,20等，
	 * 如果传入为10,即得到当前时间为:00,10,20,30,等.
	 * 
	 * @param diffMinu
	 * @return 返回当前时间包括小时与分钟
	 * @author sunanjun 2013-6-25 下午11:56:41
	 */
	public static String getNowTimeByDiff(int diffMinu) {
		String nowTimeMinu = DateTools.getNowDateTime("m");
		String nowTimeHours = DateTools.getNowDateTime("HH");
		int intMinu = 0;
		try {
			intMinu = Integer.parseInt(nowTimeMinu);
		} catch (NumberFormatException nfe) {
			intMinu = 0;
		}
		int divMinu = intMinu - intMinu % diffMinu;
		String rtn = nowTimeHours + ":" + StringTools.formatLeftPadZero(String.valueOf(divMinu), 2);
		return rtn;
	}

	/**
	 * 根据间隔时间得到每隔5分钟的时间，即得到当前时间为：05,10,15,20等，
	 * 
	 * @return
	 * @author sunanjun 2013-6-25 下午11:59:15
	 */
	public static String getNowTimeByDiff5() {
		return getNowTimeByDiff(5);
	}

	/**
	 * 根据间隔时间得到每隔10分钟的时间，即得到当前时间为：00,10,20,30等，
	 * 
	 * @return
	 * @author sunanjun 2013-6-25 下午11:59:19
	 */
	public static String getNowTimeByDiff10() {
		return getNowTimeByDiff(10);
	}

	/**
	 * 返回当前周的第一天的日期，默认周一为每周的第一天
	 * 
	 * @return
	 * @author JiangBin 2013-6-3 上午9:50:28
	 */
	public static String startDayOfWeek() {
		Calendar calendar = Calendar.getInstance();
		int min = calendar.getActualMinimum(Calendar.DAY_OF_WEEK); // 获取周开始基准,给定此Calendar的时间值,返回指定日历字段可能拥有的最小值.
		int current = calendar.get(Calendar.DAY_OF_WEEK);// 返回给定日历字段的值.
		calendar.add(Calendar.DAY_OF_WEEK, min - (current - 1)); // 基准-当天，获取周开始日期.
		Date start = calendar.getTime();
		SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
		return ymd.format(start);
	}

	/**
	 * 根据参数返回本周的某一天对应的日期，参数为1-7,否则返回为null。例如参数为1，则为本周周一对应的日期。
	 * 
	 * @param intDay
	 * @return
	 * @author JiangBin 2013-6-3 上午10:13:49
	 */
	public static String strDayOfWeek(int intDay) {
		if (intDay > 0 && intDay < 8) {
			Calendar calendar = Calendar.getInstance();
			int min = calendar.getActualMinimum(Calendar.DAY_OF_WEEK); // 获取周开始基准,给定此Calendar的时间值,返回指定日历字段可能拥有的最小值.
			int current = calendar.get(Calendar.DAY_OF_WEEK);// 返回给定日历字段的值.
			if (current == 1) {
				calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR) - 1);
			}
			calendar.add(Calendar.DAY_OF_WEEK, min - (current - 1) + (intDay - 1)); // 基准-当天，获取周开始日期.
			Date start = calendar.getTime();
			SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
			return ymd.format(start);
		} else {
			return null;
		}
	}

	/**
	 * 返回当前周的最后一天的日期，默认周一为每周的第一天
	 * 
	 * @return
	 * @author JiangBin 2013-6-3 上午9:58:28
	 */
	public static String endDayOfWeek() {
		Calendar calendar = Calendar.getInstance();
		int min = calendar.getActualMinimum(Calendar.DAY_OF_WEEK); // 获取周开始基准,给定此Calendar的时间值,返回指定日历字段可能拥有的最小值.
		int current = calendar.get(Calendar.DAY_OF_WEEK);// 返回给定日历字段的值.
		calendar.add(Calendar.DAY_OF_WEEK, min - (current - 1)); // 基准-当天，获取周开始日期.
		calendar.add(Calendar.DAY_OF_WEEK, 6); // 开始+6，获取周结束日期
		Date end = calendar.getTime();
		SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
		return ymd.format(end);
	}

	/**
	 * 返回指定某一周周几对应的日期
	 * 
	 * @param intWeek
	 * @param intDay
	 * @return
	 * @author JiangBin 2013-6-3 下午2:24:48
	 */
	public static String strDayOfWeek(int intWeek, int intDay) {
		if (intDay > 0 && intDay < 8) {
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.WEEK_OF_YEAR, intWeek);
			int min = calendar.getActualMinimum(Calendar.DAY_OF_WEEK); // 获取周开始基准,给定此Calendar的时间值,返回指定日历字段可能拥有的最小值.
			int current = calendar.get(Calendar.DAY_OF_WEEK);// 返回给定日历字段的值.
			calendar.add(Calendar.DAY_OF_WEEK, min - (current - 1) + (intDay - 1)); // 基准-当天，获取周开始日期.
			Date start = calendar.getTime();
			SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
			return ymd.format(start);
		} else {
			return null;
		}
	}

	/**
	 * 返回当前月
	 * 
	 * @return
	 * @author JiangBin 2013-6-5 上午10:46:09
	 */
	public static String getCurrentMonth() {
		Calendar calendar = Calendar.getInstance();
		return (calendar.get(Calendar.MONTH) + 1) + "";
	}

	/**
	 * 返回当前月
	 * 
	 * @return
	 * @author JiangBin 2013-6-5 上午10:46:09
	 */
	public static int getIntCurrentMonth() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * 返回当前年
	 * 
	 * @return
	 * @author JiangBin 2013-6-5 上午10:46:09
	 */
	public static int getCurrentYear() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 根据参数返回某年某月的某一天对应的日期。
	 * 
	 * @param year
	 *            ,month,intDay
	 * @return
	 * @author JiangBin 2013-6-3 上午10:13:49
	 */
	public static String strDayOfMonth(int year, int month, int intDay) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.DAY_OF_MONTH, intDay);
		Date start = calendar.getTime();
		SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
		return ymd.format(start);
	}

	/**
	 * 根据参数返回某年某月的第一天对应的日期。
	 * 
	 * @param year
	 * @param month
	 * @return
	 * @author JiangBin 2013-6-5 下午12:45:21
	 */
	public static String startDayOfMonth(int year, int month) {
		SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		Date beginTime = calendar.getTime();
		return ymd.format(beginTime);
	}

	/**
	 * 根据参数返回某年某月的最后一天对应的日期。
	 * 
	 * @param year
	 * @param month
	 * @return
	 * @author JiangBin 2013-6-5 下午12:45:21
	 */
	public static String endDayOfMonth(int year, int month) {
		SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.DATE, 1);
		calendar.roll(Calendar.DATE, -1);
		Date endTime = calendar.getTime();
		return ymd.format(endTime);
	}

	/**
	 * 获取当前周，周的第一天为周一
	 * 
	 * @return
	 * @author JiangBin 2013-6-3 上午8:45:35
	 */
	public static int getCurrentWeek() {
		int currentWeek = 0;
		if (Calendar.getInstance().get(Calendar.DAY_OF_WEEK) == 1)
			currentWeek = (Calendar.getInstance().get(Calendar.WEEK_OF_YEAR) - 1);
		else
			currentWeek = Calendar.getInstance().get(Calendar.WEEK_OF_YEAR);
		return currentWeek;
	}

	/**
	 * 当前日期加减n天后的日期，返回String   (yyyy-mm-dd)  
	 * @param n
	 * @return
	 * @author sunanjun 2013-7-2 下午2:18:26
	 */
	public static String nDaysAftertoday(int n) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar rightNow = Calendar.getInstance();
		rightNow.add(Calendar.DAY_OF_MONTH, +n);
		return df.format(rightNow.getTime());
	}

	/**
	  * 当前日期加减n天后的日期，返回String   (yyyy-mm-dd hh:mm)  
	  * @param n
	  * @return
	  * @author sujianhong 2018-10-2 下午2:18:26
	  */
	public static String nDaysAftertoday2(int n) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		Calendar rightNow = Calendar.getInstance();
		rightNow.add(Calendar.DAY_OF_MONTH, +n);
		return df.format(rightNow.getTime());
	}

	/**
	 * 当前日期加减n天后的日期，返回String (yyyy-mm-dd)
	 * @param n
	 * @return
	 * @author sunanjun 2013-7-2 下午2:18:26
	 */
	public static Date nDaysAfterNowDate(int n) {
		Calendar rightNow = Calendar.getInstance();
		rightNow.add(Calendar.DAY_OF_MONTH, +n);
		return rightNow.getTime();
	}

	/**
	 * 给定一个日期型字符串，返回加减n天后的日期型字符串  
	 * @param n
	 * @return
	 * @author sunanjun 2013-7-2 下午2:18:26
	 */
	public static String nDaysAfterOneDateString(String basicDate, int n) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date tmpDate = null;
		try {
			tmpDate = df.parse(basicDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		long nDay = (tmpDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n) * (24 * 60 * 60 * 1000);
		tmpDate.setTime(nDay);
		return df.format(tmpDate);
	}

	/**
	 * 给定一个日期，返回加减n天后的日期    
	 * @param basicDate
	 * @param n
	 * @return
	 * @author sunanjun 2013-7-2 下午2:18:26
	 */
	public static Date nDaysAfterOneDate(Date basicDate, int n) {
		long nDay = (basicDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n) * (24 * 60 * 60 * 1000);
		basicDate.setTime(nDay);
		return basicDate;
	}

	/**
	 * 计算两个日期相隔的天数 ,日期期
	 * @param firstDate
	 * @param secondDate 
	 * @return
	 * @author sunanjun 2013-7-2 下午2:18:26
	 */
	public static int nDaysBetweenTwoDate(Date firstDate, Date secondDate) {
		int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
		return nDay;
	}

	/**
	 * 计算两个日期相隔的天数  
	 * @param firstString
	 * @param secondString 
	 * @return
	 * @author sunanjun 2013-7-2 下午2:18:26
	 */
	public static int nDaysBetweenTwoDate(String firstString, String secondString) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date firstDate = null;
		Date secondDate = null;
		try {
			firstDate = df.parse(firstString);
			secondDate = df.parse(secondString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
		return nDay;
	}

	/**
	 * 将时间转换为更直观的时间
	 * 比如将"2018-06-06 15:30:30"转换为"昨天 下午 3:30:30"
	 * @author WangWeiWei 020463
	 * @date 2018年7月5日 下午2:10:31
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public static String getDirectViewTime(String dateString) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		Calendar currentCalendar = Calendar.getInstance();
		SimpleDateFormat daySfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date currentDate = new Date();
		calendar.setTime(daySfd.parse(dateString));
		currentCalendar.setTime(currentDate);
		if (calendar.get(Calendar.YEAR) == currentCalendar.get(Calendar.YEAR)) {
			String dayDesc = StringUtils.EMPTY;
			int dayPassed = currentCalendar.get(Calendar.DAY_OF_YEAR) - calendar.get(Calendar.DAY_OF_YEAR);
			if (dayPassed == 0) {
				dayDesc = "今天";
			} else if (dayPassed == 1) {
				dayDesc = "昨天";
			} else if (dayPassed == 2) {
				dayDesc = "前天";
			} else if (dayPassed >= 3 && dayPassed <= 10) {
				dayDesc = NumUtils.translate(dayPassed) + "天前";
			} else if (dayPassed == -1) {
				dayDesc = "明天";
			} else if (dayPassed == -2) {
				dayDesc = "后天";
			} else if (dayPassed <= -3 && dayPassed >= -10) {
				dayDesc = NumUtils.translate(Math.abs(dayPassed)) + "天后";
			}
			if (!"".equals(dayDesc)) {
				if (calendar.get(Calendar.HOUR_OF_DAY) >= 0 && calendar.get(Calendar.HOUR_OF_DAY) < 6) {
					return dayDesc + " 凌晨 " + new SimpleDateFormat("HH时mm分").format(daySfd.parse(dateString));
				} else if (calendar.get(Calendar.HOUR_OF_DAY) >= 6 && calendar.get(Calendar.HOUR_OF_DAY) < 9) {
					return dayDesc + " 早晨 " + new SimpleDateFormat("HH时mm分").format(daySfd.parse(dateString));
				} else if (calendar.get(Calendar.HOUR_OF_DAY) >= 9 && calendar.get(Calendar.HOUR_OF_DAY) < 11) {
					return dayDesc + " 上午 " + new SimpleDateFormat("HH时mm分").format(daySfd.parse(dateString));
				} else if (calendar.get(Calendar.HOUR_OF_DAY) >= 11 && calendar.get(Calendar.HOUR_OF_DAY) < 13) {
					return dayDesc + " 中午 " + calendar.get(Calendar.HOUR_OF_DAY) + "时" + new SimpleDateFormat("mm分").format(daySfd.parse(dateString));
				} else if (calendar.get(Calendar.HOUR_OF_DAY) >= 13 && calendar.get(Calendar.HOUR_OF_DAY) < 17) {
					return dayDesc + " 下午 " + ((calendar.get(Calendar.HOUR_OF_DAY) - 12) < 10 ? ("0" + (calendar.get(Calendar.HOUR_OF_DAY) - 12)) : (calendar.get(Calendar.HOUR_OF_DAY) - 12)) + "时"
							+ new SimpleDateFormat("mm分").format(daySfd.parse(dateString));
				} else if (calendar.get(Calendar.HOUR_OF_DAY) >= 17 && calendar.get(Calendar.HOUR_OF_DAY) < 19) {
					return dayDesc + " 黄昏 " + ((calendar.get(Calendar.HOUR_OF_DAY) - 12) < 10 ? ("0" + (calendar.get(Calendar.HOUR_OF_DAY) - 12)) : (calendar.get(Calendar.HOUR_OF_DAY) - 12)) + "时"
							+ new SimpleDateFormat("mm分").format(daySfd.parse(dateString));
				} else if (calendar.get(Calendar.HOUR_OF_DAY) >= 19 && calendar.get(Calendar.HOUR_OF_DAY) < 21) {
					return dayDesc + " 晚上 " + ((calendar.get(Calendar.HOUR_OF_DAY) - 12) < 10 ? ("0" + (calendar.get(Calendar.HOUR_OF_DAY) - 12)) : (calendar.get(Calendar.HOUR_OF_DAY) - 12)) + "时"
							+ new SimpleDateFormat("mm分").format(daySfd.parse(dateString));
				} else if (calendar.get(Calendar.HOUR_OF_DAY) >= 21) {
					return dayDesc + " 深夜 " + ((calendar.get(Calendar.HOUR_OF_DAY) - 12) < 10 ? ("0" + (calendar.get(Calendar.HOUR_OF_DAY) - 12)) : (calendar.get(Calendar.HOUR_OF_DAY) - 12)) + "时"
							+ new SimpleDateFormat("mm分").format(daySfd.parse(dateString));
				}
			}
		}
		return dateString;
	}
}
